package com.carhome.ui.fagment;

import com.carhome.R;
import com.carhome.adapter.CarFragmentAdapter;
import com.carhome.adapter.CarMainFragmentAdapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class CarFragment extends Fragment {
	private ViewPager mViewPager;
	private CarMainFragmentAdapter mAdapter; // 定义ViewPagerFfagment适配器

	/**
	 * 上边的三个tabitem及布局
	 */
	private LinearLayout mTabByBrandView;
	private LinearLayout mTabByConditionView;
	private LinearLayout mTabBySecondCarView;

	private TextView mByBrand_tv;
	private TextView mByConditon_tv;
	private TextView mBySecondCar_tv;
	private ImageView mSearchImg;

	/**
	 * TabItem下面的线条
	 */
	private ImageView mTabLine;

	private int currentIndex; // 当前处于哪个fragment
	private int screenWidth; // 屏幕宽度

	private View mView; // 解析布局

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.car_layout_main, container, false);
		setupViews();
		initTabLine();
		return mView;
	}

	private void initTabLine() {
		// 顶部bar下面的指示图片
		mTabLine = (ImageView) mView.findViewById(R.id.car_layout_tab_line);
		screenWidth = getResources().getDisplayMetrics().widthPixels;
		LinearLayout.LayoutParams params = (LayoutParams) mTabLine.getLayoutParams();
		params.width = screenWidth / 4;
		mTabLine.setLayoutParams(params);
	}

	private void setupViews() {
		mViewPager = (ViewPager) mView.findViewById(R.id.car_layout_viewpager);
		// 三个布局
		mTabByBrandView = (LinearLayout) mView.findViewById(R.id.car_layout_byBrand_view);
		mTabByConditionView = (LinearLayout) mView.findViewById(R.id.car_layout_byCondition_view);
		mTabBySecondCarView = (LinearLayout) mView.findViewById(R.id.car_layout_bySecondCar_view);

		// 为三个布局设置监听
		mTabByBrandView.setOnClickListener(myLayoutClickListener);
		mTabByConditionView.setOnClickListener(myLayoutClickListener);
		mTabBySecondCarView.setOnClickListener(myLayoutClickListener);

		// 布局上的三个TextView
		mByBrand_tv = (TextView) mView.findViewById(R.id.car_layout_byBrand_textview);
		mByConditon_tv = (TextView) mView.findViewById(R.id.car_layout_byCondition_textview);
		mBySecondCar_tv = (TextView) mView.findViewById(R.id.car_layout_bySecondCar_textview);

		CarFragmentByBrand mFragmentByBrand = new CarFragmentByBrand();
		CarFragmentByCondition mFragmentByCondition = new CarFragmentByCondition();
		CarFragmentBySecondCar mFragmentBySecondCar = new CarFragmentBySecondCar();

		mAdapter = new CarMainFragmentAdapter(getChildFragmentManager());
		mAdapter.addTab(mFragmentByBrand);
		mAdapter.addTab(mFragmentByCondition);
		mAdapter.addTab(mFragmentBySecondCar);

		mViewPager.setAdapter(mAdapter); // 为viewpager设置适配器
		mViewPager.setOnPageChangeListener(myPageChangeListener);
		
		mByBrand_tv.setTextColor(getResources().getColor(R.color.car_bar_sected_text_color));//设置初始选中颜色
	}

	private OnPageChangeListener myPageChangeListener = new OnPageChangeListener() {
		// 状态改变时回调接口
		public void onPageSelected(int position) {
			currentIndex = position;
			resetTextColor();
			switch (position) {
			case 0:
				mByBrand_tv.setTextColor(getResources().getColor(R.color.car_bar_sected_text_color));
				break;
			case 1:
				mByConditon_tv.setTextColor(getResources().getColor(R.color.car_bar_sected_text_color));
				break;
			case 2:
				mBySecondCar_tv.setTextColor(getResources().getColor(R.color.car_bar_sected_text_color));
				break;
			default:
				break;
			}
		}

		/**
		 * @param positon
		 *            当前的位置
		 * @param positionOffset
		 *            偏移比(小于1)，向左滑动(1.0-0变化)，向右滑动(0-1.0变化)
		 * @param positionOffsetPixels
		 *            具体偏移的像素
		 */
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			// 设置tabLine的左偏移
			if (currentIndex == 0 && position == 0)// 0->1
			{
				LinearLayout.LayoutParams lp = (LayoutParams) mTabLine.getLayoutParams();
				lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 4) + currentIndex
						* (screenWidth / 4));

				mTabLine.setLayoutParams(lp);
			} else if (currentIndex == 1 && position == 0) // 1->0
			{
				LinearLayout.LayoutParams lp = (LayoutParams) mTabLine.getLayoutParams();
				lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 4) + currentIndex
						* (screenWidth / 4));
				mTabLine.setLayoutParams(lp);

			} else if (currentIndex == 1 && position == 1) // 1->2
			{
				LinearLayout.LayoutParams lp = (LayoutParams) mTabLine.getLayoutParams();
				lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 4) + currentIndex
						* (screenWidth / 4));
				mTabLine.setLayoutParams(lp);
			} else if (currentIndex == 2 && position == 1) // 2->1
			{
				LinearLayout.LayoutParams lp = (LayoutParams) mTabLine.getLayoutParams();
				lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 4) + currentIndex
						* (screenWidth / 4));
				mTabLine.setLayoutParams(lp);
			}
		}

		public void onPageScrollStateChanged(int arg0) {
		}
	};

	private OnClickListener myLayoutClickListener = new OnClickListener() {
		public void onClick(View v) {
			int id = v.getId();
			switch (id) {
			case R.id.car_layout_byBrand_view:
				mViewPager.setCurrentItem(0);
				Toast.makeText(getActivity(), "第一个", Toast.LENGTH_SHORT).show();
				break;
			case R.id.car_layout_byCondition_view:
				mViewPager.setCurrentItem(1);
				break;
			case R.id.car_layout_bySecondCar_view:
				mViewPager.setCurrentItem(2);
				break;
			case R.id.car_layout_searcher_img:
				// TODO
				break;
			default:
				break;
			}
		}
	};

	private void resetTextColor() {
		mByBrand_tv.setTextColor(getResources().getColor(android.R.color.black));
		mByConditon_tv.setTextColor(getResources().getColor(android.R.color.black));
		mBySecondCar_tv.setTextColor(getResources().getColor(android.R.color.black));
	}
}
